<template>
	<div>
		<use-table ref="tbl"></use-table>
		<div class="container_status dflex_vertical_c">
			<div class="item_interval" v-for="(item, i) in stateDatas" :key="i">
				<el-button class="badge_txt" :class="{ active: item.name == state }" size="small" @click="cutSta(item)">
					{{ item.name }}
				</el-button>
				<v-countup class="badge" start-value="0" :end-value="item.cnt"></v-countup>
			</div>
		</div>
		<div class="container padding_b_0">
			<div class="dflex_wrap">
				<div class="dflex_vertical_c margin_r_40 margin_b_20">
					<div class="search_name">商品名称：</div>
					<el-input v-model="req.name" placeholder="请输入" class="search_input" @change="search" clearable>
					</el-input>
				</div>
				<el-button size="mini" :loading="searchBtnLoading" class="search-btn margin_b_20 margin_r_40"
					@click="search">搜索</el-button>
			</div>
		</div>
		<div class="container last_container use-table">
			<div class="dflex_sb margin_b_15 table-head">
				<div></div>
				<el-button class="add-btn pos_r padding0" icon="iconfont iconxinzeng" @click="toAdd">新增</el-button>
			</div>
			<el-table ref="etbl" :height="tblHeight" v-loading="tableLoading" :data="tableDatas" highlight-current-row>
				<el-table-column label="排序" width="200" align="center">
					<template slot-scope="scope">
						<el-input-number size="small" :min="1" v-model="scope.row.hot_sort"
							@change="(newSort, oldSort) => sortChange(newSort, oldSort, scope.row)"
							@blur="scope.row.hot_sort = parseInt(scope.row.hot_sort)"></el-input-number>
					</template>
				</el-table-column>
				<el-table-column label="商品" width="330" align="center">
					<template slot-scope="scope">
						<div class="dflex_vertical_c">
							<use-carousel :width="84" :height="84" :imgs="scope.row.imgs" :video="scope.row.video">
							</use-carousel>
							<div class="left_just margin_l_10">
								<el-popover popper-class="use_popover" :open-delay="200" :close-delay="40"
									placement="right" :title="scope.row.name" trigger="hover"
									:content="scope.row.name_pw || '--'">
									<div slot="reference" class="dflex_d_c">
										<div class="ellipsis crpr">{{ scope.row.name }}</div>
										<div class="ellipsis crpr">{{ scope.row.name_pw || '--' }}</div>
									</div>
								</el-popover>
							</div>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="价格" align="center">
					<template slot-scope="scope">
						<div class="price">{{ scope.row.price / 100 }}</div>
						<div class="m_price">{{ scope.row.market_price / 100 }}</div>
					</template>
				</el-table-column>
				<el-table-column property="state" label="状态" align="center"></el-table-column>
				<el-table-column label="数据统计" align="left" width="150">
					<template slot-scope="scope">
						<div class="left_just">
							<div>已售数:{{ scope.row.sale_cnt }}</div>
							<div>访问数:{{ scope.row.visit_cnt }}</div>
							<div>收藏数:{{ scope.row.collect_cnt }}</div>
							<div>分享数:{{ scope.row.share_cnt }}</div>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="热门推荐" align="center">
					<template slot-scope="scope">
						<el-tooltip :open-delay="350" :content="scope.row.hot == '1' ? '点击取消热门推荐' : '点击启用热门推荐'"
							placement="top" :hide-after="1000" :enterable="false" effect="light">
							<el-switch v-model="scope.row.hot" active-color="#ff6a6c" inactive-color="#bbb"
								active-value="1" inactive-value="0" @change="hotChange(scope.row)"></el-switch>
						</el-tooltip>
					</template>
				</el-table-column>
				<el-table-column label="标签" width="220" align="center">
					<template slot-scope="scope">
						<div class="left_just">
							<el-tag effect="plain" class="margin_t_5 margin_r_5 sele_none" type="info"
								v-for="(item, i) in scope.row.tags" :key="i">{{ item }}</el-tag>
						</div>
					</template>
				</el-table-column>
				<div slot="empty">
					<el-empty description=" "></el-empty>
				</div>
			</el-table>
			<el-pagination background :current-page="req.page" :page-sizes="[10, 20, 30, 50, 100]" :page-size="req.rows"
				layout="total, sizes, prev, pager, next, jumper" :total="tableTotal" @size-change="sizeChange"
				@current-change="currentChange"></el-pagination>
			<goods ref="goodsRef" @choiceGoods="getGoodsDatas"></goods>
		</div>
	</div>
</template>

<script>
	import vCountup from 'vue-countupjs';
	const __name = 'wmzmall-goods';
	export default {
		components: {
			vCountup
		},
		data() {
			return {
				state: '全部',
				stateObj: {
					待审核: 0,
					销售中: 0,
					已下架: 0,
					全部: 0
				},
				stateDatas: [],
				req: {
					hot: 1,
					page: 1,
					rows: 10,
					orderby: 'hot_sort asc',
					name: '',
					state: ''
				},
				tblHeight: 0,
				tableLoading: false,
				tableDatas: [],
				tableTotal: 0,
				searchBtnLoading: false
			};
		},
		methods: {
			async loadData(callback) {
				this.tableLoading = true;

				this.stateDatas = [];
				for (let key in this.stateObj) {
					this.stateDatas.push({
						name: key,
						cnt: this.stateObj[key]
					});
				}

				await this.$db[__name]
					.where({
						hot: 1
					})
					.whereif(this.req.state && this.req.state != '全部', {
						state: this.req.state
					})
					.whereif(this.req.name, {
						name: new RegExp(this.$api.filterRegexpKey(this.req.name), 'i')
					})
					.withgroup({
						field: 'state',
						obj: this.stateObj
					})
					.totable(this.req)
					.then(res => {
						if (res.code == 200) {
							if (res.datas.group && res.datas.group.datas) {
								this.stateDatas = res.datas.group.datas;
							}

							res.datas.rows.forEach(row => {
								row.hot = row.hot + '';
							});

							this.tableDatas = res.datas.rows;
							this.tableTotal = res.datas.total;

							if (typeof callback === 'function') {
								callback();
							}
						}
						this.searchBtnLoading = false;
						this.tableLoading = false;
					});
			},
			search() {
				this.req.page = 1;
				this.$api.storagePaginationPage(this.req.page, this.$route.path);
				this.searchBtnLoading = true;

				this.loadData(() => {
					this.$refs.etbl.bodyWrapper.scrollTop = 0;
				});

				uni.setStorage({
					key: '__search' + this.$route.path,
					data: this.req
				});
			},
			toAdd() {
				this.$refs.goodsRef.show({
					hot: 0,
					state: '销售中'
				});
			},
			getGoodsDatas(datas) {
				let ids = datas.map(x => x._id);
				if (ids && ids.length > 0) {
					this.$db[__name]
						.where(`in(_id, ${JSON.stringify(ids)})`)
						.update('', {
							hot: 1
						})
						.then(res => {
							if (res.code == 200) {
								this.loadData(() => {
									this.$refs.etbl.bodyWrapper.scrollTop = 0;
								});
							}
						});
				}
			},
			sortChange(newSort, oldSort, row) {
				if (!newSort) {
					this.$nextTick(() => {
						row.hot_sort = oldSort;
					})
					return;
				}

				if (event && ['change', 'mouseup'].includes(event.type)) {
					this.sortUpdate(row);
					return;
				}

				if (this.sortLastTime !== null) {
					clearTimeout(this.sortLastTime)
				}

				this.sortLastTime = setTimeout(() => {
					this.sortUpdate(row)
				}, 500);
			},
			sortUpdate(row) {
				this.$db[__name].update(row._id, {
					hot_sort: parseInt(row.hot_sort)
				}).catch(() => {
					this.loadData();
				})
			},
			hotChange(row) {
				this.$db[__name].update(row._id, {
					hot: parseInt(row.hot)
				}).catch(() => {
					this.loadData();
				})
			},
			cutSta(item) {
				uni.setStorage({
					key: '__cutSta' + this.$route.path,
					data: item
				});

				this.state = item.name;
				this.req.state = item.name;
				this.req.page = 1;
				this.$api.storagePaginationPage(this.req.page, this.$route.path);

				this.loadData(() => {
					this.$refs.etbl.bodyWrapper.scrollTop = 0;
				});
			},
			sizeChange(size) {
				this.$api.storagePaginationRows(size, this.$route.path);
				this.req.page = 1;
				this.$api.storagePaginationPage(this.req.page, this.$route.path);
				this.req.rows = size;

				this.loadData(() => {
					this.$refs.etbl.bodyWrapper.scrollTop = 0;
				});
			},
			currentChange(current) {
				this.$api.storagePaginationPage(current, this.$route.path);
				this.req.page = current;

				this.loadData(() => {
					this.$refs.etbl.bodyWrapper.scrollTop = 0;
				});
			}
		},
		created() {
			let __search = uni.getStorageSync('__search' + this.$route.path);
			if (__search) {
				this.req = __search;
			}

			let thisCutSta = uni.getStorageSync('__cutSta' + this.$route.path);
			this.state = '全部';
			this.req.page = 1;
			this.req.state = this.state;
			if (thisCutSta) {
				this.state = thisCutSta.name;
				this.req.state = thisCutSta.name;
			}
			this.req.page = uni.getStorageSync('__paginationPage' + this.$route.path) || 1;
			this.req.rows = uni.getStorageSync('__paginationRows' + this.$route.path) || 10;

			this.loadData(() => {
				this.$nextTick(() => {
					this.$api.scrollToStorage('.el-table__body-wrapper', this.$route.path);
				});
			});
		},
		mounted() {
			this.$nextTick(() => {
				this.$api.scrollOnStorage('.el-table__body-wrapper', this.$route.path);
			});
		},
		updated() {
			if (!this.tblHeight) {
				this.tblHeight = this.$refs.tbl.tblHeight;
			}
		}
	};
</script>

<style></style>
